home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14109 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.2 KB  |  144 lines

  1. Path: nntp0.brunel.ac.uk!usenet
  2. From: cspgrgr@brunel.ac.uk (R Ghosh-Roy)
  3. Newsgroups: comp.lang.c
  4. Subject: sizeof + offsetof for browsing structures
  5. Date: 11 Apr 1996 16:52:38 GMT
  6. Organization: Brunel University, Uxbridge, UK
  7. Distribution: world
  8. Message-ID: <4kjdcm$e4l@mimas.brunel.ac.uk>
  9. Reply-To: cspgrgr@brunel.ac.uk
  10. NNTP-Posting-Host: molnir.brunel.ac.uk
  11. Keywords: sizeof, offsetof, structure, schema
  12.  
  13.  
  14.  
  15. Hello Everybody,
  16.  
  17. I am a bit confused about "pointer arithmatic" and therefore would appreciate
  18. your expert help. The following is the problem:
  19.  
  20. I am defining a schema (or templates) using the following structure
  21. definitions:
  22.  
  23. typedef struct SchemaStruc
  24. {
  25.   char *name ;
  26.  
  27.   List entities ;
  28. } ;
  29.  
  30. typedef struct EntityStruc
  31. {
  32.   int id ;
  33.   char *name, description ;
  34.   long int strucsize ;
  35.  
  36.   List attributes ;
  37. } ;
  38.  
  39. typedef struct AttributeStruc
  40. {
  41.   int id ;
  42.   char *name, description ;
  43.   long int fieldoffset ;
  44. } ;
  45.  
  46.  
  47.                 /                  /
  48. schema --------|-- entity --------|-- attribute
  49.                 \                  \
  50.        1-many             1-many
  51.  
  52. I have defined three entities A,B and C. For example, entity A defines
  53. a house and one creates lots of instances of A stored in an array. The
  54. instances of B (people) and C (address) are also stored in arrays.
  55.  
  56. |--|
  57. |  |-->A1->A2->A3->A4->etc             (instances of entity A)
  58. |  |
  59. |  |-->B1->B2->B3->B4->B5->B6->B7->etc (instances of entity B)
  60. |  |
  61. |  |-->C1->C2->C3->C4->C5->etc         (instances of entity C)
  62. |--|
  63.  
  64.  
  65. Each entity (say of size X using "sizeof") has many attributes and each 
  66. attribute comes with an offset length (say Y using "offsetof") within the 
  67. entity. I can then search for any attribute instance within an array of 
  68. entity instances just by doing pointer arithmatic:
  69.  
  70.                entity Z
  71.           -    |--------|  -
  72.           |    |        |  |
  73.           |    |        |  Y offset
  74.           |    |        |  |
  75.      size X    |        |  |
  76.           |    | Za     |  -
  77.           |    |        |
  78.           -    |--------|
  79.  
  80.  
  81.     /** Get X (sizeof) from the chosen entity definition **/
  82.     /** Get Y (offsetof) from the chosen entity-attribute definition **/
  83.  
  84.   for (x=start_of_array; 
  85.        x < number_of_instances * X; 
  86.        x+=X)
  87.   {
  88.     x+Y points directly to the attribute value I am after.
  89.  
  90.     
  91.   }
  92.  
  93. This avoids me writing accessing function for each attribute. The program 
  94. looks at the entity structure size and the field offset at the meta level 
  95. and then uses pointer arithmatic to traverse the entity instances and its 
  96. attribute instances. The beauty is that as its the same for all attributes,
  97. I dont need individual function for accessing each attribute definition.
  98.  
  99.  
  100. However, I now wish to add ANOTHER attribute which points to other entities.
  101.  
  102.       entity A
  103.       |--------|
  104. . . . |        |  . . . . . . . . . . . . . . . . 
  105.       |        |
  106.       |        |
  107.       |        |        entity B
  108.       | Ab  ---|------->|--------|
  109.       |        |        |        |
  110.       |--------|        |        | 
  111. . . . . . . . . . . . . |        | . . . . . . . . . . . . . . . . 
  112.                         |        |
  113.                         |        |         entity C
  114.                         | Bc  ---|-------->|----------|
  115.                         |        |         |          |
  116.                         |        |         |          |
  117.                         |        |         |          |
  118.                         |--------|         |          |
  119.                                            |          |
  120.                                            |  Cd      |
  121.                                            |          |
  122.  . . . . . . . . . . . . . . . . . . . . . |          |  . . . . . . . . . . 
  123.                                            |          |
  124.                                            |          |
  125.                                            |----------|
  126.  
  127. WARNING: The solution to the following question is NOT trivial.
  128. My question is: can I use sizeof and offsetof to get to Cd as I 
  129. can do to get to Ab (described above)? Ie, *NOT* write specific 
  130. functions for each attribute (Cd) to access its value by traversing 
  131. down from entity A, but use only sizeof and offsetof.
  132.  
  133. Thanks,
  134.  
  135. Rana
  136.  
  137.  
  138. -- 
  139. R. Ghosh-Roy, Research Fellow @ BIPS  -- R.Ghosh-Roy@brunel.ac.uk -- Extension 2772
  140.  
  141.  
  142.  
  143.  
  144.